/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yurendao.shop.forum.service.impl;

import com.alibaba.druid.sql.visitor.functions.Char;
import com.yurendao.shop.forum.dao.CommentMapper;
import com.yurendao.shop.forum.dao.NoteMapper;
import com.yurendao.shop.forum.dao.NotePictureMapper;
import com.yurendao.shop.forum.domain.*;
import com.yurendao.shop.forum.exception.CommentException;
import com.yurendao.shop.forum.exception.NoteException;
import com.yurendao.shop.forum.service.ForumService;
import com.yurendao.shop.pager.domain.ClassifyBean;
import com.yurendao.shop.pager.domain.Page;
import com.yurendao.shop.pager.domain.PageControl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author shilin
 */
@Service
public class ForumServiceImpl implements ForumService {

    @Autowired
    private NoteMapper noteMapper;
    @Autowired
    private NotePictureMapper notePictureMapper;
    @Autowired
    private CommentMapper commentMapper;


    @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false, isolation = Isolation.DEFAULT)
    @Override
    public void insertNote(Note note) throws NoteException {
        ForumValidate.noteCheck(note);
        noteMapper.insert(note);
    }

    @Override
    public void deleteNoteByNid(String nid) throws NoteException {
        noteMapper.deleteByPrimaryKey(nid);
    }

    /**
     * 显示帖子列表
     *
     * @return
     * @throws com.yurendao.shop.forum.exception.NoteException
     */
    @Override
    public List<Note> notelist(String orderByClause,int pageOffset) throws NoteException {
        NoteExample noteExample = new NoteExample();
        noteExample.setOrderByClause(orderByClause);
        noteExample.setPc(pageOffset);
        noteExample.setPs(15);
        return noteMapper.selectByExample(noteExample);
    }

    @Override
    public Page<Note> selectNoteByKeyWord(String keyWord, String orderByClause, int pageOffset) throws NoteException {
        NoteExample noteExample = new NoteExample();
        noteExample.createCriteria().andTitleLike(keyWord).andContentLike(keyWord);
        noteExample.setOrderByClause(orderByClause);
        int pc = Page.getPc(pageOffset);
        int ps = Page.getPs(pageOffset);
        noteExample.setPc(pc);
        noteExample.setPs(ps);
        List<Note> notes = noteMapper.selectOrExample(noteExample);
        int totalcount = noteMapper.countOrExample(noteExample);
        Page<Note> notePage = new Page<Note>(notes,pageOffset,totalcount);
        return notePage;
    }


    @Override
    public void insertComment(Comment comment) throws CommentException {
        commentMapper.insert(comment);
    }

    @Override
    public void deleteCommentByCid(String cid) throws CommentException {
        commentMapper.deleteByPrimaryKey(cid);
    }

    @Override
    public Page<Comment> selectCommentByNote(String nid, String orderByClause, Integer pageOffset) throws CommentException {
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andNoteidEqualTo(nid);
        int pc = Page.getPc(pageOffset);
        int ps = Page.getPs(pageOffset);
        commentExample.setPc(pc);
        commentExample.setPs(ps);
        commentExample.setOrderByClause(orderByClause);
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        CommentExample count = new CommentExample();
        count.createCriteria().andNoteidEqualTo(nid);
        int pageSize = commentMapper.countByExample(count);
        Page<Comment> commentPage = new Page<Comment>(comments,pageOffset,pageSize);
        return commentPage;
    }


    @Override
    public Note displayNoteById(String nid) throws CommentException {
        Note note = noteMapper.selectByPrimaryKey(nid);
        return note;
    }

    @Override
    public int getCommentCountByNoteId(String nid) throws NoteException {
        return noteMapper.countCommentByNoteId(nid);
    }

}
